import Rx from 'rxjs';
import update from 'react/lib/update';
import CounterIntent from "../intent/CounterIntent";
import JsonIntent from "../intent/JsonIntent";

const subject = new Rx.ReplaySubject(1);

let state = {
  postResult: [],
  results: [],
  counter: 0
};

CounterIntent.subjects.incrementCounterSubject.subscribe(()=> {
  state = update(state, {
    $merge: {
      counter: state.counter + 1
    }
  });
  subject.next(state);
});

CounterIntent.subjects.decreaseCounterSubject.subscribe(()=> {
  state = update(state, {
    $merge: {
      counter: state.counter - 1
    }
  });
  subject.next(state);
});

JsonIntent.subjects.goGetJSON.subscribe(data => {
  state = update(state, {
    $merge: {
      results: data
    }
  });
  subject.next(state);
});

JsonIntent.subjects.goPostJSON.subscribe(data => {
  state = update(state, {
    $merge: {
      postResult: data
    }
  });
  subject.next(state);
});

subject.next(state);

export default {
  subject
};
